package org.jeecg.modules.degression.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.degression.mapper.*;
import org.jeecg.modules.degression.pojo.MlccCategoryInfo;
import org.jeecg.modules.degression.pojo.MlccPriceSystemFactoryRecord;
import org.jeecg.modules.degression.pojo.MlccPricesystemFactoryCategory;
import org.jeecg.modules.degression.pojo.SysUser;
import org.jeecg.modules.degression.service.IFactoryDegressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FactoryDegressServiceImpl implements IFactoryDegressService {

    @Autowired
    FactoryCategoryMapper factoryCategoryMapper;

    @Autowired
    FactoryRecordMapper factoryRecordMapper;

    @Autowired
    MyUserMapper userMapper;

    @Autowired
    MyMlccCategoryInfoMapper2 categoryInfoMapper2;

    @Autowired
    MlccPriceSystemPingleiMapper priceSystemPingleiMapper;

    /**
     * 供应商新增成本
     *
     * @param category 品类ID
     * @param userid   用户id
     */
    @Override
    public int saveDegress(String category, String userid) {
        SysUser user = userMapper.selectById(userid);

        // 新增之前先检查是否之前添加过
        QueryWrapper<MlccPricesystemFactoryCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_user_id", userid).eq("category_id", category);
        Integer count = factoryCategoryMapper.selectCount(wrapper);
        if (count == 0) {
            // 如果有成本存在，不能添加
            int insert = 0;
            for (int i = 0; i < 3; i++) {
                MlccPricesystemFactoryCategory category1 = new MlccPricesystemFactoryCategory();
                category1.setSysUserId(userid).setCategoryId(category).setIsGongyi(0).setStatus(0).setCreateBy(user.getRealname())
                        .setUpdateBy(user.getRealname()).setSysOrgCode(user.getOrgCode());
                if (i == 0) {
                    category1.setBusinessType(0).setBusinessName("品类个订成本");
                } else if (i == 1) {
                    category1.setBusinessType(1).setBusinessName("品类团订成本");
                } else if (i == 2) {
                    category1.setBusinessType(2).setBusinessName("品类标准码成本");
                }
                insert = factoryCategoryMapper.insert(category1);
            }
            return insert;
        } else {
            return -1;
        }
    }

    /**
     * 查询成本项列表
     *
     * @param page         分页器
     * @param category     类别
     * @param businessType 业务类型
     */
    @Override
    public IPage<MlccPricesystemFactoryCategory> listPrice(Page<MlccPricesystemFactoryCategory> page, Integer category, Integer businessType, String userid) {
        QueryWrapper<MlccPricesystemFactoryCategory> wrapper = new QueryWrapper<>();
        if (category != null) {
            wrapper.eq("category_id", category);
        }
        if (businessType != null) {
            wrapper.eq("business_type", businessType);
        }
        wrapper.orderByDesc("create_time");
        IPage<MlccPricesystemFactoryCategory> price = factoryCategoryMapper.listPrice2(page, category, businessType, userid);
        for (MlccPricesystemFactoryCategory record : price.getRecords()) {
            QueryWrapper<MlccPriceSystemFactoryRecord> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("pricesystem_factory_id", record.getId());
            Integer count = factoryRecordMapper.selectCount(wrapper1);
            if (count > 0) {
                record.setStatus(2);
            } else {
                record.setStatus(0);
            }
        }
        return price;
    }

    /**
     * 删除不需要的成本项
     *
     * @param id 成本id
     */
    @Override
    public int delPriceC(String id) {
        return factoryCategoryMapper.deleteById(id);
    }

    /**
     * 保存系统加急成本项(公用)
     *
     * @param costID       关联的参数的数据ID，如绣字的ID，绣字高度的id
     * @param typeID       类型：0定制项，1定制项下的参数，2绣字工艺，3撞色，4绣字部位，5绣字高度，6返修参数，7加急项
     * @param systemID     上级主键ID
     * @param systemCateID 成本管理的类别ID
     * @param categoryName 加急项名称
     * @param price        价格
     */
    @Override
    public int saveurgent(String costID, Integer typeID, String systemID, String systemCateID, String categoryName, String price, String costCode) {
        Double aDouble = Double.valueOf(price);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 插入数据
        QueryWrapper<MlccPriceSystemFactoryRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("cost_item", systemCateID).eq("pricesystem_factory_id", systemID).eq("cost_name", categoryName).eq("cost_id", costID);
        MlccPriceSystemFactoryRecord one = factoryRecordMapper.selectOne(wrapper);
        // 判断是否可能重复添加
        if (one == null) {
            MlccPriceSystemFactoryRecord record = new MlccPriceSystemFactoryRecord();
            record.setCostItem(systemCateID).setCostName(categoryName).setPrice(BigDecimal.valueOf(aDouble)).setType(typeID)
                    .setCostCode(costCode)
                    .setStatus(1).setPricesystemFactoryId(systemID).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname()).setCostId(costID);
            return factoryRecordMapper.insert(record);
        } else {
            return -1;
        }
    }

    /**
     * 获取保存的返修项
     *
     * @param page     分页器
     * @param systemID 上级ID
     * @param typeID   具体的成本类型ID
     */
    @Override
    public IPage<MlccPriceSystemFactoryRecord> listChengben(Page<MlccPriceSystemFactoryRecord> page, String systemID, String typeID) {
        QueryWrapper<MlccPriceSystemFactoryRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("pricesystem_factory_id", systemID);
        if ("2".equals(typeID)) {
            wrapper.in("type", 2, 4, 5);
        } else {
            wrapper.eq("type", typeID);
        }
        wrapper.orderByDesc("create_time");
        IPage<MlccPriceSystemFactoryRecord> iPage = factoryRecordMapper.selectPage(page, wrapper);
        List<MlccPriceSystemFactoryRecord> records = iPage.getRecords();
        for (MlccPriceSystemFactoryRecord record : records) {
            MlccCategoryInfo category = priceSystemPingleiMapper.getCategory(record.getCostItem());
            if (category != null) {
//                record.setCateogryName(categoryInfoMapper2.selectById(record.getCostItem()).getCategoryName());
                record.setCateogryName(category.getCategoryName());
            }
        }
        return iPage;
    }

    /**
     * 更新成本价
     *
     * @param id    成本数据ID
     * @param price 价格
     */
    @Override
    public int updatePrice(String id, String price) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        MlccPriceSystemFactoryRecord record = factoryRecordMapper.selectById(id);
        record.setPrice(BigDecimal.valueOf(Double.valueOf(price))).setUpdateBy(user.getRealname());
        return factoryRecordMapper.updateById(record);
    }

    /**
     * 保存定制成本项
     *
     * @param systemID     上级主键ID
     * @param systemCateID 成本管理的类别ID
     * @param categoryID   类别ID
     * @param categoryName 类别名称
     * @param price        价格
     */
    @Override
    public int savePricesavePrice(String systemID, String systemCateID, String categoryID, String categoryName, String price, String costCode) {
        Double aDouble = Double.valueOf(price);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 1、首先更新类别表的数据
        MlccCategoryInfo categoryInfo = categoryInfoMapper2.selectById(categoryID);
        if (categoryInfo != null) {
            categoryInfo.setIsSet(1).setUpdateBy(user.getRealname());
            categoryInfoMapper2.updateById(categoryInfo);
        }
        // 插入数据
        QueryWrapper<MlccPriceSystemFactoryRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("pricesystem_factory_id", systemID).eq("cost_name", categoryName);
        MlccPriceSystemFactoryRecord one = factoryRecordMapper.selectOne(wrapper);
        if (one == null) {
            MlccPriceSystemFactoryRecord record = new MlccPriceSystemFactoryRecord();
            record.setCostItem(systemCateID).setCostName(categoryName).setPrice(BigDecimal.valueOf(aDouble)).setType(1).setCostId(categoryID)
                    .setCostCode(costCode)
                    .setStatus(1).setPricesystemFactoryId(systemID).setCreateBy(user.getRealname()).setUpdateBy(user.getRealname());
            return factoryRecordMapper.insert(record);
        } else {
            return -1;
        }
    }

    /**
     * 删除成本项
     *
     * @param id 成本项id
     */
    @Override
    public int delChengben(String id) {
        return factoryRecordMapper.deleteById(id);
    }
}
